﻿using System.Collections;
using CoopSaude.Controllers;
using System;
using Castle.MonoRail.Framework;

namespace CoopSaude.ViewComponent
{

    public class PartialComponent : Castle.MonoRail.Framework.ViewComponent
    {
        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Render()
        {
            try
            {
                string action = null;
                string partial = null;
                string controller = null;
                string area = null;
                string partialFromLayout = null;
                string partialFromArea = null;
                string invokeAction = null;

                foreach (DictionaryEntry entry in Context.ComponentParameters)
                {
                    switch (entry.Key.ToString().ToLower())
                    {
                        case "action":
                            action = entry.Value.ToString();
                            break;

                        case "partial":
                            partial = "_" + entry.Value.ToString();
                            break;

                        case "area":
                            area = entry.Value.ToString();
                            break;

                        case "controller":
                            controller = entry.Value.ToString();
                            break;

                        case "invokeaction":
                            invokeAction = entry.Value.ToString();
                            break;

                        case "partialfromlayout":
                            partialFromLayout = entry.Value.ToString();
                            break;

                        case "partialfromarea":
                            partialFromArea = entry.Value.ToString();
                            break;

                        default:
                            this.EngineContext.CurrentControllerContext.PropertyBag[entry.Key] = entry.Value;
                            break;

                    }
                }

                BaseController baseController = null;
                Type type = null;

                if (controller != null)
                {
                    controller = controller.Trim();

                    type = Type.GetType("CoopSaude.Controllers." + controller + "Controller", true, true);
                    baseController = (BaseController)Activator.CreateInstance(type);

                    baseController.SetEngineContext(this.EngineContext);
                    baseController.ControllerContext = this.EngineContext.CurrentControllerContext;
                    if (area == null)
                    {
                        if (baseController.AreaName != null)
                            area = baseController.AreaName;
                    }
                }
                else
                {
                    baseController = (BaseController)this.EngineContext.CurrentController;
                    type = baseController.GetType();

                    controller = baseController.Name;
                    if (baseController.AreaName != null)
                        area = baseController.AreaName;
                }

                if (invokeAction != null)
                {
                    if (Convert.ToBoolean(invokeAction))
                        type.InvokeMember(action, System.Reflection.BindingFlags.InvokeMethod, null, baseController, null);
                }


                System.IO.StringWriter sw = new System.IO.StringWriter();

                if(partialFromLayout == "true")
                {
                   this.EngineContext.Services.ViewEngineManager.ProcessPartial(System.IO.Path.Combine("layouts", partial), sw, this.EngineContext, baseController, this.EngineContext.CurrentControllerContext);
                }
                else if (partialFromArea == "true")
                {
                    this.EngineContext.Services.ViewEngineManager.ProcessPartial(System.IO.Path.Combine(area, partial), sw, this.EngineContext, baseController, this.EngineContext.CurrentControllerContext);
                } 
                else 
                {
                    // verifica se o controller pertence a alguma area, pois se pertencer a partial esta numa pasta dentro da pasta da area
                    if (area.Length > 0)
                        this.EngineContext.Services.ViewEngineManager.ProcessPartial(System.IO.Path.Combine(area, System.IO.Path.Combine(controller, partial)), sw, this.EngineContext, baseController, this.EngineContext.CurrentControllerContext);
                    else
                        this.EngineContext.Services.ViewEngineManager.ProcessPartial(System.IO.Path.Combine(controller, partial), sw, this.EngineContext, baseController, this.EngineContext.CurrentControllerContext);
                }
                RenderText(sw.ToString());
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }
    }
}
